﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Threading;

using AKMII.DMRA.Business.Management;
using AKMII.DMRA.Common;
using AKMII.DMRA.Business.Service;

namespace AKMII.DMRA.Web
{
    public partial class ForgetPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnContinue_Click(object sender, EventArgs e)
        {
            string validateCode = Session["ValidateCode"] as string;
            AccountManager accountManager = new AccountManager();

            if (txtCharacter.Text.Equals(validateCode, StringComparison.CurrentCultureIgnoreCase))
            {
                if (accountManager.CheckUser(txtUsername.Text, txtEmail.Text))
                {
                    // to do reset password and send mail.
                    string password = Utility.BuildPassword(8);

                    if (accountManager.ResetUser(txtUsername.Text, 
                        FormsAuthentication.HashPasswordForStoringInConfigFile(password, Constants.EncryptSetting)))
                    {
                        Mail mail = new Mail();
                        mail.Subject = "New password";
                        mail.Content = "Your password is reset to " + password;
                        mail.Recipients = new List<string>() { txtEmail.Text.Trim() };

                        Thread thread = new Thread(new ParameterizedThreadStart(SendPassword));
                        thread.Start(mail);

                        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(),
                            "resetSuccess",
                            "<script type='text/javascript'>alert('The password is sent to you, please check your mail box');</script>");
                    }
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(),
                        "userError", "<script type='text/javascript'>alert('Please input correct Agent Code and Mail Address');</script>");
                }
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(),
                    "codeError", "<script type='text/javascript'>alert('Please input correct validate code');</script>");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Constants.LoginUrl);
        }

        protected void SendPassword(object o)
        {
            Mail mail = o as Mail;
            if (mail != null)
            {
                CommonUtility.SendMail(mail);
            }
        }
    }
}
